import { Component, OnInit } from '@angular/core';
import { News } from '../../model/news';
import { NewsService } from '../../service/news.service';
import { Router, ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-newslist',
  templateUrl: './newslist.component.html',
  styleUrls: ['./newslist.component.css']
})
export class NewslistComponent implements OnInit {
  nl:Array<News>;
  constructor(
    private newsservice:NewsService,
    private router:Router,
    private routerinfo:ActivatedRoute
  ) { }

  ngOnInit() {
    this.loadNewsList();
  }

  private loadNewsList(){
    this.nl = new Array();
    this.newsservice.getNewsList()
    .then((data:any)=>{
      this.nl = data;
    })
  }


  addNews(){
    this.router.navigate(['editnews','add']);
  }


  updateNews(id:string){
    this.router.navigate(['editnews',id]);
  }


  deleteNews(id:string){
    this.newsservice.deleteNews(id)
    .then((flag:boolean)=>{
      if(flag==true){
        this.loadNewsList();
      }
      else{
        alert("删除异常,请刷新后重试");
      }
    })
  }

  toDetail(id:string){
    this.router.navigate(['detail',id]);
  }
}
